package com.ruoyi.mall.service;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.MallCategory;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.mapper.MallCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Service
public class MallCategoryService {
    @Autowired
    private MallCategoryMapper mallCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param cateId 商品分类主键
     * @return 商品分类
     */
    public MallCategory selectMallCategoryByCateId(Long cateId) {
        return mallCategoryMapper.selectMallCategoryByCateId(cateId);
    }

    /**
     * 查询商品分类列表
     *
     * @param mallCategory 商品分类
     * @return 商品分类
     */
    public List<MallCategory> selectMallCategoryList(MallCategory mallCategory) {
        return mallCategoryMapper.selectMallCategoryList(mallCategory);
    }

    public List<TreeSelect> buildTreeSelect(List<MallCategory> categories) {
        List<MallCategory> menuTrees = buildTree(categories);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public List<MallCategory> buildTree(List<MallCategory> categories) {
        List<MallCategory> returnList = new ArrayList<>();
        List<Long> tempList = categories.stream().map(MallCategory::getCateId).collect(Collectors.toList());
        for (MallCategory category : categories) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId())) {
                recursionFn(categories, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty()) {
            returnList = categories;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t    子节点
     */
    private void recursionFn(List<MallCategory> list, MallCategory t) {
        // 得到子节点列表
        List<MallCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (MallCategory tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<MallCategory> getChildList(List<MallCategory> list, MallCategory t) {
        List<MallCategory> tlist = new ArrayList<>();
        Iterator<MallCategory> it = list.iterator();
        while (it.hasNext()) {
            MallCategory n = it.next();
            if (n.getParentId().longValue() == t.getCateId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<MallCategory> list, MallCategory t) {
        return !getChildList(list, t).isEmpty();
    }

    /**
     * 新增商品分类
     *
     * @param mallCategory 商品分类
     * @return 结果
     */
    public int insertMallCategory(MallCategory mallCategory) {
        mallCategory.setCreateTime(DateUtils.getNowDate());
        mallCategory.setCreateBy(SecurityUtils.getUsername());
        return mallCategoryMapper.insertMallCategory(mallCategory);
    }

    /**
     * 修改商品分类
     *
     * @param mallCategory 商品分类
     * @return 结果
     */
    public int updateMallCategory(MallCategory mallCategory) {
        mallCategory.setUpdateTime(DateUtils.getNowDate());
        mallCategory.setUpdateBy(SecurityUtils.getUsername());
        return mallCategoryMapper.updateMallCategory(mallCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param cateIds 需要删除的商品分类主键
     * @return 结果
     */
    public int deleteMallCategoryByCateIds(Long[] cateIds) {
        return mallCategoryMapper.deleteMallCategoryByCateIds(cateIds);
    }

    /**
     * 删除商品分类信息
     *
     * @param cateId 商品分类主键
     * @return 结果
     */
    public int deleteMallCategoryByCateId(Long cateId) {
        return mallCategoryMapper.deleteMallCategoryByCateId(cateId);
    }
}
